import Point from "./Point.js";
import Config from "./Config.js";

export default class Area{
    constructor(x,y,width,height){
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    isPointInArea(p){
        if(p.x >= this.x + 1 && p.y >= this.y + 1 && p.x <= this.x + this.width - 1 && p.y <= this.y + this.height - 1){
            return true;
        }
        return false;
    }
    isCollision(a){
        let p1 = new Point(a.x,a.y);
        let p2 = new Point(a.x,a.y+a.height);
        let p3 = new Point(a.x+a.width,a.y);
        let p4 = new Point(a.x+a.width,a.y+a.height);
        let p5 = new Point(a.x+a.width / 2, a.y);
        let p6 = new Point(a.x, a.y + a.height / 2);
        let p7 = new Point(a.x + a.width, a.y + a.height / 2);
        let p8 = new Point(a.x + a.width / 2, a.y + a.height);

        let collision = new Area(this.x + Config.BlockBorder,this.y + Config.BlockBorder,this.width - Config.BlockBorder, this.height - Config.BlockBorder);

        if(
            collision.isPointInArea(p1) || 
            collision.isPointInArea(p2) || 
            collision.isPointInArea(p3) || 
            collision.isPointInArea(p4) ||
            collision.isPointInArea(p5) ||
            collision.isPointInArea(p6) ||
            collision.isPointInArea(p7) ||
            collision.isPointInArea(p8)
            ){
            return true;
        }
        return false;
    }
}